package cn.yujian95.hospital.controller;


import cn.yujian95.hospital.common.api.CommonPage;
import cn.yujian95.hospital.common.api.CommonResult;
import cn.yujian95.hospital.dto.param.HospitalCmsParam;
import cn.yujian95.hospital.dto.param.HospitalInfoParam;
import cn.yujian95.hospital.entity.HospitalCms;
import cn.yujian95.hospital.entity.HospitalInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "医院模块", tags = "轮播图接口")
@RestController
@RequestMapping("/hospital")
public class HospitalCmsController {

    @Resource
    private cn.yujian95.hospital.service.HospitalCmsService hospitalCmsService;

    @ApiOperation(value = "添加轮播图信息", notes = "传入 轮播图信息参数（图片地址、排序）")
    @RequestMapping(value = "/cms/info", method = RequestMethod.POST)
    public CommonResult insertHospitalCmsInfo(@RequestBody HospitalCmsParam param) {


        if (hospitalCmsService.insert(param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }


    @ApiOperation(value = "查看轮播图信息", notes = "传入 轮播图信息参数（轮播图编号）")
    @RequestMapping(value = "/cms/info/{id}", method = RequestMethod.GET)
    public CommonResult<HospitalCms> getHospitalCmsInfo(@PathVariable("id") Long id) {

        try {
            if (hospitalCmsService.countCms(id)) {
                HospitalCms hospitalCms = hospitalCmsService.getHospitalCmsInfo(id);
                return CommonResult.success(hospitalCms);
            } else {
                return CommonResult.failed("该轮播图不存在！");
            }
        } catch (Exception e) {
            return CommonResult.failed("服务器错误，请联系管理员！");
        }


    }


    @ApiOperation(value = "更新轮播图信息", notes = "传入 轮播图编号、轮播图信息参数（名称，图片、地址，简介）")
    @ApiImplicitParam(name = "id", value = "轮播图编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/cms/info/{id}", method = RequestMethod.PUT)
    public CommonResult updateHospitalCmsInfo(@PathVariable("id") Long id, @RequestBody HospitalCmsParam param) {


        if (!hospitalCmsService.countCms(id)) {
            return CommonResult.validateFailed("不存在，该轮播图编号！");
        }

        if (hospitalCmsService.updateCms(id, param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }


    @ApiOperation(value = "删除轮播图信息", notes = "传入 轮播图编号")
    @ApiImplicitParam(name = "id", value = "轮播图编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/cms/info/{id}", method = RequestMethod.DELETE)
    public CommonResult deleteHospitalInfo(@PathVariable("id") Long id) {


        if (!hospitalCmsService.countCms(id)) {
            return CommonResult.validateFailed("不存在，该轮播图编号！");
        }

        if (hospitalCmsService.deleteCms(id)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }


    @ApiOperation(value = "分页：搜索轮播图信息", notes = "传入 医院名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "轮播图名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true)
    })
    @RequestMapping(value = "/cms/info/list", method = RequestMethod.GET)
    public CommonResult<IPage<HospitalCms>> searchHospitalCmsInfo(@RequestParam(required = false) String name,
                                                  @RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize) {

        IPage<HospitalCms> list = hospitalCmsService.listWithQuery(name, pageNum, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "查询全部轮播图信息", notes = "")
    @RequestMapping(value = "/cms/info/listAll", method = RequestMethod.GET)
    public CommonResult<List<HospitalCms>> searchHospitalCmsInfoList() {

        List<HospitalCms> list = hospitalCmsService.searchHospitalCmsInfoList();
        return CommonResult.success(list);
    }





}
